package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.entity.*;
import com.project.myBeans.ResponseBean;
import com.project.myBeans.TeacherInfoDetail;
import com.project.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * author: WheelChen
 * data: 2017/6/24
 * function:教师信息相关操作
 */
@Controller
@RequestMapping("/teacherInfo")
public class TeacherInfoController {
    @Autowired
    private TeacherInfoService teacherInfoService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private UniversityService universityService;
    @Autowired
    private UserService userService;


    @RequestMapping(value = "/addDetailOne", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/6/23
     * function: 根据id更新用户表信息 创建教师信息表信息
     *
     * @param teacherInfoDetail 教师详细信息（用户表 + 教师信息表）
     * @return
     */
    public ResponseBean addDetailOne(@RequestBody TeacherInfoDetail teacherInfoDetail) {
        ResponseBean responseBean = new ResponseBean();
        try {
            teacherInfoService.addDetailOne(teacherInfoDetail);
            responseBean.setMessage("添加成功");
            responseBean.setSuccess(false);
        } catch (Exception e) {
            responseBean.setMessage("程序异常");
            responseBean.setSuccess(false);
        }
        return responseBean;

    }

    @RequestMapping(value = "/modifyDetailOne", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/6/23
     * function: 根据id更新用户表信息 根据id更新教师信息表
     *
     * @param teacherInfoDetail 教师详细信息（用户表 + 教师信息表）
     * @return
     */
    public ResponseBean modifyDetailOne(@RequestBody TeacherInfoDetail teacherInfoDetail) {
        ResponseBean responseBean = new ResponseBean();
        try {
            teacherInfoService.modifyDetailOne(teacherInfoDetail);
            responseBean.setMessage("修改成功");
            responseBean.setSuccess(true);
        } catch (Exception e) {
            responseBean.setMessage("程序异常");
            responseBean.setSuccess(false);
        }
        return responseBean;

    }

    @RequestMapping(value = "/findOneDetail/{userId}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/6/23
     * function: 按userId查找教师信息
     *
     * @param userId
     * @return
     */
    public ResponseBean findOneDetail(@PathVariable("userId") Integer userId) {
        ResponseBean responseBean = new ResponseBean();
        try {
            TeacherInfoDetail teacherInfoDetail = teacherInfoService.findOneDetailByUserId(userId);
            responseBean.setData(teacherInfoDetail);
            responseBean.setMessage("查找成功");
            responseBean.setSuccess(true);
            return responseBean;
        } catch (Exception e) {
            responseBean.setMessage("查找失败");
            responseBean.setSuccess(false);
            return responseBean;
        }
    }

    @RequestMapping(value = "/findBatchByUniversityid/{universityid}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/7/15
     * function: 根据大学id批量查询教师信息
     *
     * @param universityid 大学id
     * @return
     */
    public ResponseBean findBatchByUniversityid(@PathVariable("universityid") Integer universityid){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.setData(teacherInfoService.findBatchByUniversityid(universityid));
            responseBean.setSuccess(true);
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;

    }

    //Todo 按照学院查找老师
    @RequestMapping(value = "/findDetailBatchBySchoolIdCondition", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/7/26
     * function: 根据schoolid查找老师详细信息
     *
     * @param teacherInfo
     * @return
     */
    public ResponseBean findDetailBatchBySchoolIdCondition(@RequestBody TeacherInfo teacherInfo){
        ResponseBean responseBean = new ResponseBean();
        try {
            teacherInfo.setPageIndex(teacherInfo.getPageIndex() - 1);
            List<TeacherInfoDetail> teacherInfoDetailList = teacherInfoService.findBatchBySchoolIdCondition(teacherInfo);
            responseBean.setData(teacherInfoDetailList);
            responseBean.setTotal(teacherInfo.getTotal());
            responseBean.setSuccess(true);
            responseBean.setMessage("查找成功");
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }



}
